package com.liqi.web.controller.deployment;

import com.liqi.common.annotation.Log;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.common.enums.BusinessType;
import com.liqi.common.utils.DateUtils;
import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.deployment.domain.DvDeploymentModule;
import com.liqi.deployment.service.IDvDeploymentModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部署设备模块Controller
 *
 * @author xiaoli
 * @date 2022-02-14
 */
@Api(tags = "设备部署管理")
@RestController
@RequestMapping("/device/model")
public class DvDeploymentModuleController extends BaseController
{
    @Autowired
    private IDvDeploymentModuleService dvDeploymentModuleService;

    /**
     * 根据中间件生成的唯一device_id查询部署设备模块列表
     */
    @ApiOperation("部署设备模块：根据中间件生成的唯一device_id查询部署设备模块列表")
    //@PreAuthorize("@ss.hasPermi('device:model:listBydeploymentId')")
    @GetMapping("/listBydeploymentId")
    public TableDataInfo listBydeploymentId(String deviceId)
    {
        startPage();
        List<DvDeploymentModule> list = dvDeploymentModuleService.selectDvDeploymentModuleByDeviceId(deviceId);
        return getDataTable(list);
    }

    /**
     * 查询部署设备模块列表
     */
    @ApiOperation("部署设备模块：查询部署设备模块列表")
    @PreAuthorize("@ss.hasPermi('device:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(DvDeploymentModule DvDeploymentModule)
    {
        startPage();
        List<DvDeploymentModule> list = dvDeploymentModuleService.selectDvDeploymentModuleList(DvDeploymentModule);
        return getDataTable(list);
    }

    /**
     * 导出部署设备模块列表
     */
    @ApiOperation("部署设备模块：导出部署设备模块列表")
    @PreAuthorize("@ss.hasPermi('device:model:export')")
    @Log(title = "部署设备模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DvDeploymentModule DvDeploymentModule)
    {
        List<DvDeploymentModule> list = dvDeploymentModuleService.selectDvDeploymentModuleList(DvDeploymentModule);
        ExcelUtil<DvDeploymentModule> util = new ExcelUtil<DvDeploymentModule>(DvDeploymentModule.class);
        util.exportExcel(response, list, "部署设备模块数据");
    }

    /**
     * 获取部署设备模块详细信息
     */
    @ApiOperation("部署设备模块：获取部署设备模块详细信息")
    @PreAuthorize("@ss.hasPermi('device:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dvDeploymentModuleService.selectDvDeploymentModuleById(id));
    }

    /**
     * 新增部署设备模块
     */
    @ApiOperation("部署设备模块：新增部署设备模块")
    @PreAuthorize("@ss.hasPermi('device:model:add')")
    @Log(title = "部署设备模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DvDeploymentModule DvDeploymentModule)
    {
        DvDeploymentModule.setCreateBy(getUsername());
        DvDeploymentModule.setCreateTime(DateUtils.getNowDate());
        DvDeploymentModule.setUpdateBy(getUsername());
        DvDeploymentModule.setUpdateTime(DateUtils.getNowDate());
        DvDeploymentModule.setStatus(0);
        return toAjax(dvDeploymentModuleService.insertDvDeploymentModule(DvDeploymentModule));
    }

    /**
     * 修改部署设备模块
     */
    @ApiOperation("部署设备模块：修改部署设备模块")
    @PreAuthorize("@ss.hasPermi('device:model:edit')")
    @Log(title = "部署设备模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DvDeploymentModule DvDeploymentModule)
    {
        DvDeploymentModule.setUpdateBy(getUsername());
        DvDeploymentModule.setUpdateTime(DateUtils.getNowDate());
        return toAjax(dvDeploymentModuleService.updateDvDeploymentModule(DvDeploymentModule));
    }

    /**
     * 删除部署设备模块
     */
    @ApiOperation("部署设备模块：删除部署设备模块")
    @PreAuthorize("@ss.hasPermi('device:model:remove')")
    @Log(title = "部署设备模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = 0;
        /*for (long id : ids) {
            DvDeploymentModule DvDeploymentModule = dvDeploymentModuleService.selectDvDeploymentModuleById(id);
            if (DvDeploymentModule != null) {
                DvDeploymentModule.setUpdateBy(getUsername());
                DvDeploymentModule.setUpdateTime(DateUtils.getNowDate());
                DvDeploymentModule.setStatus(1);
                i = dvDeploymentModuleService.updateDvDeploymentModule(DvDeploymentModule);
            }
        }*/
        i = dvDeploymentModuleService.deleteDvDeploymentModuleByIds(ids);
        return toAjax(i);
    }
}
